/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.util;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JComponent;

/**
 *
 * @author Glauco
 */
public class BaliComponentsRow {

    JComponent firstColumnComponent;
    JComponent[] secondColumnComponents;
    JComponent firstComponentOfSecondColumn;
    JComponent lastComponent;
    GroupLayout layout;

    public BaliComponentsRow() {
        firstColumnComponent = null;
        secondColumnComponents = null;
        firstComponentOfSecondColumn = null;
        lastComponent = null;
    }

    public BaliComponentsRow(GroupLayout layout) {
        this();
        this.layout = layout;
    }

    public void addComponent(JComponent component){
        if (component == null)
            throw new RuntimeException("JComponent can not be null!");

        if (firstComponentOfSecondColumn == null)
            firstComponentOfSecondColumn = component;

        JComponent[] aux;
        if ( secondColumnComponents == null)
            aux = new JComponent[1];
        else{
            aux = new JComponent[secondColumnComponents.length + 1];

            for (int i = 0; i < secondColumnComponents.length; i++) {
                aux[i] = secondColumnComponents[i];
            }
        }

        aux[aux.length - 1] = component;

        secondColumnComponents = aux;

        lastComponent = component;
    }

    public SequentialGroup getGroupOfComponentes() {
        return getGroupOfComponentes(layout);
    }

    public SequentialGroup getGroupOfComponentes(GroupLayout layout) {
        SequentialGroup group = layout.createSequentialGroup();
        boolean gap = false;
        for (JComponent component : secondColumnComponents) {
            if(gap)
                group.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
            else
                gap = true;
            group.addComponent(component,
                    javax.swing.GroupLayout.PREFERRED_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.PREFERRED_SIZE);
        }
        return group;
    }

    public ParallelGroup getBaselineGroup(){
        return getBaselineGroup(layout);
    }

    public ParallelGroup getBaselineGroup(GroupLayout layout){
        ParallelGroup group = layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE);
        group.addComponent(firstColumnComponent);
        if (secondColumnComponents != null)
            for (JComponent component : secondColumnComponents) {
                group.addComponent(component,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE);
            }
        return group;
    }

    public int getComponentesSize(){
        if (secondColumnComponents != null)
            return secondColumnComponents.length;
        return 0;
    }

    public JComponent getFirstColumnComponent() {
        return firstColumnComponent;
    }

    public void setFirstColumnComponent(JComponent firstColumnComponent) {
        this.firstColumnComponent = firstColumnComponent;
    }

    public JComponent getFirstComponentOfSecondColumn() {
        return firstComponentOfSecondColumn;
    }

    public void setFirstComponentOfSecondColumn(JComponent firstComponentOfSecondColumn) {
        this.firstComponentOfSecondColumn = firstComponentOfSecondColumn;
    }

    public JComponent getLastComponent() {
        return lastComponent;
    }

    public void setLastComponent(JComponent lastComponent) {
        this.lastComponent = lastComponent;
    }

    public GroupLayout getLayout() {
        return layout;
    }

    public void setLayout(GroupLayout layout) {
        this.layout = layout;
    }

    public JComponent[] getSecondColumnComponents() {
        return secondColumnComponents;
    }

    public void setSecondColumnComponents(JComponent[] secondColumnComponents) {
        this.secondColumnComponents = secondColumnComponents;
    }
}
